package jdk8;


import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 依赖jdk1.8的日期工具类
 */
public class Jdk8DateUtil {

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT = "yyyyMMddHHmmss";
    public static final String FORMAT_YMD_HMS = "yyyy年MM月dd日HH时mm分ss秒";

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DAY_FORMAT2 = "yyyyMMdd";
    public static final String OUTPUT_FORMAT = "yyyy年MM月dd日";

    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String TIME_FORMAT2 = "HHmmss";

    private Jdk8DateUtil() {
    }

    public static LocalDateTime convertDateToLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static Date convertLocalDateTimeToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate convertDateToLocalDate(Date date) {
        return convertDateToLocalDateTime(date).toLocalDate();
    }

    public static Date convertLocalDateToDate(LocalDate time) {
        return Date.from(time.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    /******************************************************************************/
    public static Date strDate(String str, String format) {
        if (StringUtils.isEmpty(str)) {
            return nowDate();
        }
        if (StringUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }
        LocalDateTime localDateTime = LocalDateTime.parse(str, DateTimeFormatter.ofPattern(format));
        return convertLocalDateTimeToDate(localDateTime);
    }

    public static Date strDate(String str) {
        return strDate(str, null);
    }

    public static String dateStr(Date date, String format) {
        if (null == date) {
            return now(format);
        }
        if (StringUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }
        LocalDateTime localDateTime = convertDateToLocalDateTime(date);
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    public static String dateStr(Date date) {
        return dateStr(date, null);
    }

    public static String now(String format) {
        if (StringUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    public static String now() {
        return now(null);
    }

    public static Date nowDate() {
        return convertLocalDateTimeToDate(LocalDateTime.now());
    }

    public static Date toDay() {
        return convertLocalDateToDate(LocalDate.now());
    }

    public static String toDayString() {
        return LocalDate.now().format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
    }

    public static Date getNextDate(Date date) {
        return getDate(date, 1);
    }

    public static Date getPreDate(Date date) {
        return getDate(date, -1);
    }

    public static Date getDate(Date date, int n) {
        LocalDateTime localDateTime = convertDateToLocalDateTime(date);
        localDateTime.plusDays(n);
        return convertLocalDateTimeToDate(localDateTime);
    }

    public static void main(String[] args) {
        Date now = toDay();
        Date date = getPreDate(now);
        System.out.println(dateStr(date));
    }

}
